using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Assets.Scripts;
using UnityEngine;
using UnityEngine.UI;
public class ShopView : UIBase
{
    public ShopItem iteml;
    public Transform Root, TypeRoot;
    public Button Closet, typeBtn;
   // private Button butfind,butfen;
    List<ShopItem> list = new List<ShopItem>();
    private InputField inpt;
    
    //分类
    string[] typerss = { "技能", "皮肤", "宠物", "脚印" ,"表情"};
    List<ShopData> shopDatas;
    //分类

    int count=20;
    public static ShopView ins;
    void Awake()
    {
        ins = this;
    }
    public override void Init()
    {
        
        ShopModel.ins.Init();

        //商店item实例
        for (int i = 0; i < 10; i++)
        {
            Debug.Log(i);
            ShopItem item = Instantiate(iteml, Root);
            list.Add(item);
            
        }
        iteml.gameObject.SetActive(false);
        //商店item实例


        //分类按钮实例
        for (int i = 0; i < ShopModel.ins.typeBtncount; i++)
        {
            int typeindex = i;//下标
            typeBtn.transform.GetChild(1).GetComponent<Text>().text = typerss[i];
            typeBtn.transform. GetChild(0).transform.GetChild(0).GetComponent<Image>().sprite =Resources.Load<Sprite>("Goods/7110" + i);
            var item = Instantiate(typeBtn, TypeRoot);
            item.onClick.AddListener(() =>
            {
                RefTypeData(typeindex);//刷新下标
            });
        }
        typeBtn.gameObject.SetActive(false);


        RefTypeData(0);
        base.Init();
    }

    public void Refresh()
    {
        for (int i = 0; i < 10; i++)
        {
            if (i < shopDatas.Count)
            {
                list[i].RefreshData(shopDatas[i]);
            }
            else
            {
                list[i].RefreshData(null);
            }
        }
    }

    void Start()
    {
        Closet.onClick.AddListener(() =>
        {
            UIManager.ins.CloseUI("UIPanel/UiShop");
        });
        // butfind.onClick.AddListener(() =>
        // {
        //     FindName();

        // });
        // butfen.onClick.AddListener(() =>
        // {
        //      Sort();
        // });
    }

    private void RefTypeData(int typeBtnData)//分类
    {
        shopDatas = ShopModel.ins.GetType(typeBtnData);

        Refresh();
    }
    /// <summary>
    /// 查找
    /// </summary>
    public void FindName()
    {
        var findtext = inpt.text;
        Debug.Log("查找");
        if (string.IsNullOrEmpty(findtext))
        {
             Refresh();
            return;
        }
        else
        {
            List<ShopData> lists = new List<ShopData>();
            foreach (var item in ShopModel.ins.dic.Values)
            {
                if (item.name.Contains(findtext))
                {
                    lists.Add(item);
                }
            }
            for (int i = 0; i < count; i++)
            {
                if (i < lists.Count)
                {
                    list[i].RefreshData(lists[i]);
                }
                else
                {
                    list[i].RefreshData(null);
                }
            }
            
        }
    }
    public void Sort()
    {
        Debug.Log("排序");

        var sortedItems = ShopModel.ins.dic.OrderByDescending(pair => pair.Value.num).ToList();
        for (int i = 0; i < count; i++)
        {
            if (sortedItems.Contains(sortedItems[i]))
            {
                list[i].RefreshData(sortedItems[i].Value);
            }
            else
            {
                list[i].RefreshData(null);
            }
        }
    }
}
